package com.jspgou.cms.action.admin.main;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.Alipay;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.Payment;
import com.jspgou.cms.entity.Shipments;
import com.jspgou.cms.entity.ShopAdmin;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.manager.ShipmentsMng;
import com.jspgou.cms.web.threadvariable.AdminThread;

/**
* This class should preserve.
* @preserve
*/
@Controller
public class AlipayAct extends Alipay {
	/**
	 * 后台发货
	 * @param trade_no 支付宝交易号
	 * @param bean
	 * @param id
	 * @param request
	 * @param invoiceNo 物流发货单号
	 * @param freight 运费
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/order/o_sendGoods.do")
	public String shipments(String trade_no,Shipments bean,Long id, HttpServletRequest request,
			String invoiceNo,Double freight,HttpServletResponse 
			response, ModelMap model) {
		//把请求参数打包成数组
		Order order=manager.findByTradeNo(trade_no);
		if(order==null){
			order=manager.findById(id);
		}
		ShopAdmin admin = AdminThread.get();
		bean.setShopAdmin(admin);
		bean.setIndent(order);
		order.setInvoiceNo(invoiceNo);
		Double total=order.getTotal();
		if(order.getFreight()!=null){
			total=total-order.getFreight();
		}
		order.setFreight(freight);
		total+=freight;
		order.setTotal(total);
		if(order.getPayment().getId()==2){
			//配送方式为未配送，状态为已确认，支付状态为已支付
			if(order.getShippingStatus()==1&&order.getStatus()==2&&order.getPaymentStatus()==2){
				order.setShippingStatus(2);
				shipmentMng.save(bean);
				manager.updateByUpdater(order);
				if(order.getPayment()!=null){
		        	if(order.getPayment().getCode().equals("alipayPartner")){
		        		try {
							alipay(order.getPayment(),order,invoiceNo);
						} catch (Exception e) {
							e.printStackTrace();
						}		
		        	}
		        }
			}
		}
		model.addAttribute("order", order);
		return "order/view";
	}	
	
	private String alipay(Payment payment,Order order,String waybill) throws Exception{
////////////////////////////////////请求参数//////////////////////////////////////

		//支付宝交易号
		String trade_no = order.getTradeNo();
		//必填

		//物流公司名称
		String logistics_name = order.getShipping().getName();
		//必填

		//物流发货单号
		String invoice_no = waybill;
		//物流运输类型
		String transport_type = "EXPRESS";
		//三个值可选：POST（平邮）、EXPRESS（快递）、EMS（EMS）
		//////////////////////////////////////////////////////////////////////////////////
		//把请求参数打包成数组
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", "send_goods_confirm_by_platform");
        sParaTemp.put("partner", payment.getPartner());
        sParaTemp.put("_input_charset", "utf-8");
		sParaTemp.put("trade_no", trade_no);
		sParaTemp.put("logistics_name", logistics_name);
		sParaTemp.put("invoice_no", invoice_no);
		sParaTemp.put("transport_type", transport_type);
		
		//建立请求
		String sHtmlText = buildRequest("", "", sParaTemp,payment.getSellerKey());
		return sHtmlText;
	}
	
	@Autowired
	private OrderMng manager;
	@Autowired
	private ShipmentsMng shipmentMng;
	
}
